package com.atguigu.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet{

    private  BookService bookService = new BookServiceImpl();
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("经过了前台的ClientBookServlet程序");
        //             1.获取请求的参数pageNo和pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.page_size);

        System.out.println(request.getParameter("pageNo"));
        System.out.println(request.getParameter("pageSize"));

//             2.调用BookService.page(pageNo,pageSize):Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        page.setUrl("client/bookServlet?action=page");
//             3.保存page到request域中
        request.setAttribute("page",page);

//             4.请求转发到pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);

    }

    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("经过了前台的ClientBookServlet程序");
        //             1.获取请求的参数pageNo和pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.page_size);
        int min = WebUtils.parseInt(request.getParameter("min"),0);
        int max = WebUtils.parseInt(request.getParameter("max"), Integer.MAX_VALUE);

        System.out.println(request.getParameter("pageNo"));
        System.out.println(request.getParameter("pageSize"));

//             2.调用BookService.page(pageNo,pageSize):Page对象
        Page<Book> page = bookService.pageByPrice(pageNo,pageSize,min,max);

        StringBuilder sb = new StringBuilder("client/bookServlet?action=pageByPrice");
if (request.getParameter("min")!=null){
//    如果有最小价格的参数，追加到分页条的参数中
    sb.append("&min=").append(request.getParameter("min"));
}

        if (request.getParameter("max")!=null){
//    如果有最大价格的参数，追加到分页条的参数 中
            sb.append("&max=").append(request.getParameter("max"));
        }
        page.setUrl(sb.toString());
//             3.保存page到request域中
        request.setAttribute("page",page);

//             4.请求转发到pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);

    }
}
